<template>
  <div id="home">
    <el-container style="border: 1px solid #eee" class="occ">
      <el-header style="height: 50px;">
        <h4 style="margin:0;display:inline;">Mall Admin</h4>
        <el-dropdown style="float: right; font-size: 12px">
          <i class="el-icon-setting" style="margin-right: 15px"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item style="padding:0">
              <span style="display:block;padding:0 20px;text-align:center" @click="userinfo()">个人信息</span>
            </el-dropdown-item>
            <el-dropdown-item style="padding:0">
              <span style="display:block;padding:0 20px;text-align:center" @click="logout()">退出</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <el-container>
        <el-aside :width="collapse?'64px':'180px'">
          <div class="toggle-button" @click="toggleMenu">| | |</div>
          <el-menu
            :unique-opened="true"
            background-color="#485f7b"
            text-color="#eee"
            active-text-color="#588ff5"
            router
            :collapse="collapse"
            :collapse-transition="false"
            :default-active="defaultActive"
          >
            <el-submenu :index="item.id+'.'" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="iconsObj[item.id]"></i><span v-show="!collapse">{{item.authName}}</span>
              </template>
              <el-menu-item-group v-for="children in item.children" :key="children.id">
                <el-menu-item :index="'/'+children.path" @click="changeActive()">{{children.authName}}</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
    data(){
        return{
            menuList:[],
            iconsObj:{
              '125':'iconfont icon-user',
              '103':'iconfont icon-tijikongjian',
              '101':'iconfont icon-shangpin',
              '102':'iconfont icon-danju',
              '145':'iconfont icon-baobiao'
            },
            collapse:false,
            defaultActive:''
        }
    },
created () {
    this.getMenuList();
    this.defaultActive=this.$route.path
},
  methods: {
    async getMenuList(){
        const {data:res} = await this.$axios.get('menus');
        if (res.meta.status !== 200) return this.$message.err(res.meta.msg);
        this.menuList=res.data;
        console.log(this.menuList)
    },
    userinfo() {
      this.$router.push({ path: "/home" });
    },
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //切换菜单折叠
    toggleMenu(){
      this.collapse=!this.collapse;
    },
    changeActive(){
      this.defaultActive=this.$route.path;
      //给二级菜单里面的路由添加此菜单的高亮
      if(this.defaultActive=='/good/add') this.defaultActive="/good"
    }
  }
};
</script>
<style scoped>
#home >>> .el-header {
  background-color: #142438;
  color: #ccc;
  line-height: 50px;
}

#home {
  margin: 0;
  width: 100%;
  height: 100%;
}
#home >>> .el-container {
  height: 100%;
}
#home >>> .el-aside{
  background: #485f7b;
}
#home >>> .el-menu-item-group__title{
    padding: 0;
}
#home >>> .el-menu{
    border: none;
}
#home >>> .el-menu li{
  min-width: 180px;
}
.iconfont{
  margin: 0 5px;
  color: #ccc;
}
.toggle-button{
  background: #253850;
  color: #ccc;
  height: 26px;
  line-height: 24px;
  text-align: center;
  font-size: 12px;
}
.toggle-button:hover{
  cursor: pointer;
  background: #213146;
}
</style>